﻿using aichat.Data;
using aichat.Services;
using aichat.ViewModels;
using aichat.Views;
using Microsoft.EntityFrameworkCore;
using Prism.Ioc;
using Prism.Unity;
using System;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using System.IO;

namespace aichat
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            try
            {
                var mainWindow = Container.Resolve<MainWindow>();
                if (mainWindow == null)
                {
                    MessageBox.Show("无法解析MainWindow", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return null;
                }
                return mainWindow;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"创建主窗口失败: {ex.Message}\n\n异常类型: {ex.GetType().Name}\n\n堆栈跟踪:\n{ex.StackTrace}", 
                    "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return null;
            }
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            try
            {
                // 注册数据库上下文
                containerRegistry.RegisterSingleton<ApplicationDbContext>();


                // 注册基本服务
                containerRegistry.RegisterSingleton<IUserService, UserService>();
                containerRegistry.RegisterSingleton<IChatService, ChatService>();
                containerRegistry.RegisterSingleton<IPaymentService, PaymentService>();
                containerRegistry.RegisterSingleton<ICabinetInfoService, CabinetInfoService>();
                containerRegistry.RegisterSingleton<IAiApiService, AiApiService>();

                // 注册视图模型
                containerRegistry.RegisterForNavigation<ChatView, ChatViewModel>();
                containerRegistry.RegisterForNavigation<LoginView, LoginViewModel>();
                containerRegistry.RegisterForNavigation<VipView, VipViewModel>();

                // 注册主窗口视图模型
                containerRegistry.Register<MainWindowViewModel>();
                
                // 注册主窗口
                containerRegistry.Register<MainWindow>();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"注册类型失败: {ex.Message}\n\n异常类型: {ex.GetType().Name}\n\n堆栈跟踪:\n{ex.StackTrace}", 
                    "注册错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        protected override async void OnStartup(StartupEventArgs e)
        {
            try
            {
                // 确保数据库目录存在
                var dbDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SqliteDemo");
                if (!Directory.Exists(dbDirectory))
                {
                    Directory.CreateDirectory(dbDirectory);
                }
                
                base.OnStartup(e);

                // 初始化数据
           
                // 创建数据库上下文并确保数据库存在
                var context = Container.Resolve<ApplicationDbContext>();
                context.Database.EnsureCreated();



            }
            catch (Exception ex)
            {
                MessageBox.Show($"应用程序启动失败: {ex.Message}\n\n异常类型: {ex.GetType().Name}\n\n堆栈跟踪:\n{ex.StackTrace}", 
                    "启动错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
